package com.weipeep.sys.controller;


import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weipeep.sys.domain.MessageDO;
import com.weipeep.sys.service.MessageService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-05-19 23:51:13 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/message")
public class MessageController {
	@Autowired
	private MessageService messageService;
	
	@GetMapping()
	@RequiresPermissions("sys:message:message")
	String Message(){
	    return "sys/message/message";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:message:message")
	public Result<Page<MessageDO>> list(Integer pageNumber, Integer pageSize, MessageDO messageDTO){
		// 查询列表数据
        Page<MessageDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<MessageDO> wrapper = new QueryWrapper<MessageDO>(messageDTO);
        page = (Page<MessageDO>) messageService.page(page, wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:message:add")
	String add(){
	    return "sys/message/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:message:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MessageDO message = messageService.getById(id);
		model.addAttribute("message", message);
	    return "sys/message/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:message:add")
	public Result<String> save( MessageDO message){
		if(messageService.save(message)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:message:edit")
	public Result<String>  update( MessageDO message){
		messageService.updateById(message);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:message:remove")
	public Result<String>  remove( Long id){
		if(messageService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:message:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		messageService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
